package cn.edu.gcu.ssl.goods.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.gcu.ssl.goods.dao.WorkerMapper;
import cn.edu.gcu.ssl.goods.entity.Goods;
import cn.edu.gcu.ssl.goods.service.GoodsService;
import cn.edu.gcu.ssl.goods.service.WorkerService;
import cn.edu.gcu.ssl.system.service.SendMsgService;
import cn.edu.gcu.ssl.system.util.GoodsValidtor;
import cn.edu.gcu.ssl.system.util.U2id;
@Controller
@RequestMapping(value="/goods")
public class GoodsController {
	
	@Autowired
	private GoodsService goodService;
	@Autowired
	private WorkerService workerService;
	@Autowired
	private SendMsgService sendMsgService;
	
	@InitBinder  
	public void initBinder(WebDataBinder webDataBinder){  
	    webDataBinder.addValidators(new GoodsValidtor());  
	} 

	
	@ResponseBody	
	@RequestMapping(value="/findList")
	public Map<String, Object>findList(@RequestParam(required=false,value="type")String type,
			@RequestParam(required=false,value="searches")String searches,HttpServletRequest request)
	{
		Map<String, Object>map=new HashMap<String,Object>();
		if(request.getSession().getAttribute("openid")==null)
		{
			map.put("result", "error");
			return map;
			
		}
		String openid=request.getSession().getAttribute("openid").toString();//辨认用户
		List<Goods> list=goodService.findList(type,searches);
		map.put("openid",openid);
		if(list.isEmpty())
		{
			map.put("data",null);
			map.put("result", "error");
		}
		else {
			map.put("data",list);
			map.put("result", "success");
			Boolean worker=workerService.select(openid);
			map.put("worker", worker);
			
		}
		return map;
		
	}
	
	

	
	@ResponseBody	
	@RequestMapping(value="/save")
	public Map<String, Object>save(@Valid Goods goods,@RequestParam(required=false,value="uname")String uname,BindingResult bindingResult,
								   @RequestParam(required=false,value="notifyWho")String notifyWho,HttpServletRequest request)
	{	//时间，类型
		 Map<String, Object>map=new HashMap<String,Object>();
		 if(bindingResult.hasErrors()){  
			  map.put("error",bindingResult.getFieldError().getDefaultMessage());  //可注释掉
			  map.put("result", "error");
		      return map;  
		   }  
		if(request.getSession().getAttribute("openid")==null)
		{
			map.put("result", "error");
			return map;
			
		}
		
		String openid=request.getSession().getAttribute("openid").toString();
		goods.setUserId(openid);
		goods.setId(U2id.getUUID());
		if(uname!=null&&uname.equals("1"))
		{
			
			String username=request.getSession().getAttribute("username").toString();
			goods.setUserName(username);
		}
	
		if(goodService.insert(goods))
		{		
			map.put("result", "success");
			if(notifyWho!=null)
			{	
				String text=null;
				if(goods.getName()==null||goods.getName().equals(""))
				{
				 	text="亲，有新的丢失招领信息，请查看";
					
				}
				else {
					String type="新-丢失：";
					if(goods.getType()=="0")
					{
						type="新-招领：";		
					}	
					text=type+goods.getName();
				}
				sendMsgService.sendMsLink(notifyWho,text);	
			}	
		}
		else {
			map.put("result", "error");
		}
		
		
		return map;
		
	}	
	


		@ResponseBody	
		@RequestMapping(value="/delete")
		public Map<String, Object>delete(@RequestParam(required=false,value="id")String id,HttpServletRequest request)
		{
			Map<String, Object>map=new HashMap<String,Object>();		
			if(request.getSession().getAttribute("openid")==null)
			{
				map.put("result", "error");
				return map;
				
			}
				
			if(goodService.deleteByPrimaryKey(id))
			{
				map.put("result", "success");
			
			}
			else {
				map.put("result", "error");
			}
					
			return map;
			
		}
		


}

